package com.thinkingstar.iads.datacenter.entity.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.formula.functions.T;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignMajor;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignPhase;
import com.thinkingstar.iads.datacenter.entity.DcProjectMajorType;

/**
 * @ClassName: DaoDcProjectDesignMajor 
 * @Description:数据中心_项目_项目设计专业维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignMajor extends EntityRepositoryServiceImpl<DcProjectDesignMajor>{

	public DaoDcProjectDesignMajor(Dao dao) {
		super(dao);
	}
	
	/**
	 * 根据专业code查询有效的专业
	 * @param code
	 * @return
	 */
	public List <DcProjectDesignMajor> queryMajorByCode(String code){
		Criteria cri = Cnd.NEW();
		cri.where()
		.and("project_design_major_code", "=", code)
		.and("del_flag", "=", false);
		List <DcProjectDesignMajor> list = query(cri);
		return list;
	}
	
	/**
	 * 查询有效的专业
	 * @return
	 */
	public List <DcProjectDesignMajor> queryMajorByValid(){
		Criteria cri = Cnd.NEW();
		cri.where()
		.and("del_flag", "=", false);
		List <DcProjectDesignMajor> list = query(cri);
		return list;
	}
	
	
	/**
	 * 查询关联关系是否存在
	 * @param designTypeId
	 * @param designMajorId
	 * @return
	 */
	public List <DcProjectMajorType> queryMajorTypeByCondition(String designTypeId , String designMajorId){
		Sql sqls = Sqls.queryEntity("select * from dc_project_major_type where project_design_major_id = @major_id and project_design_type_id = @type_id and del_flag = @del_flag ");
		sqls.setEntity(dao().getEntity(DcProjectMajorType.class));
		sqls.params().set("major_id", designMajorId).set("type_id", designTypeId).set("del_flag", false);
		dao().execute(sqls);
		return sqls.getList(DcProjectMajorType.class);
	}

	
	
	/**
	 * 删除项目设计专业(逻辑删除),并删除专业与设计类型的关联关系
	 */
	public int clearDesignMajorTypeAndMajor(String major_id){
		DcProjectDesignMajor dcProjectDesignMajor = dao().fetch(DcProjectDesignMajor.class,major_id);
		dcProjectDesignMajor.setDel_flag(true);
		dao().update(dcProjectDesignMajor);
		List <T> list = (List <T>)dao().clearLinks(dcProjectDesignMajor,"designTypes");
		return list.size();
	}
	
	/**
	 * 设计专业更新
	 * @param dcProjectDesignMajor
	 * @return
	 */
	public int updateDesignMajor(DcProjectDesignMajor dcProjectDesignMajor){
		return updateObj(dcProjectDesignMajor);
	}
	
	
	/**
	 * 根据条件分页查询设计专业
	 * @param nutMap
	 * @return
	 */
	public QueryResult findDesignMajorPageListAndInfoByCondition(NutMap nutMap){
		List <DcProjectDesignMajor> page_list = null;
		Pager pager = null;
		Criteria cri = Cnd.NEW();
		cri.where().and("del_flag", "=", false);
		if(nutMap.get("code") != null){
			cri.where().and("project_design_major_code", "=", nutMap.get("code"));
		}
		if(nutMap.get("name") != null){
			cri.where().andLike("project_design_major_name", "%"+nutMap.get("name")+"%");
		}
		cri.getOrderBy().desc("create_time");
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			page_list = dao().query(DcProjectDesignMajor.class, cri, pager);
			pager.setRecordCount(dao().count(DcProjectDesignMajor.class,cri));
		}else{
			page_list = dao().query(DcProjectDesignMajor.class, cri);
		}
		return new QueryResult(page_list,pager);
	}
	
	
	

}
